package com.zhongyu.controller;

import com.zhongyu.pojo.Provider;
import com.zhongyu.service.ProviderService;
import com.zhongyu.vo.PageResult;
import com.zhongyu.vo.ResultVo;
import lombok.var;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/sys/provider")
public class ProviderController {
    @Resource
    private ProviderService providerService;

    @GetMapping("/list")
    public ResultVo list(){
        List<Provider> list= providerService.searchAll();
        return  ResultVo.success(list);

    }

    @GetMapping("/list/{pageNo}/{pageSize}")
    public ResultVo list(@PathVariable Integer pageNo, @PathVariable Integer pageSize, Provider provider){
        PageResult pageResult= providerService.searchAll(pageNo,pageSize,provider);
        return  ResultVo.success(pageResult);

    }
    @PostMapping("/add")
    public ResultVo add(@RequestBody Provider provider){
        try {
            provider.setCreationDate(LocalDateTime.now());
            providerService.add(provider);
            return ResultVo.success("供应商添加成功！");
        }catch (Exception ex){
            return ResultVo.failure("供应商添加失败！",ex);
        }
    }
    @PostMapping("addYuan")
    public ResultVo addYuan(@RequestBody Provider provider){
      /*  provider.search*/
        return ResultVo.success("成功");
}

    @GetMapping("/del")
    public ResultVo del(Integer id) {
        try {
            providerService.removeById(id);
            return ResultVo.success("删除用户!");
        } catch (Exception ex) {
            return ResultVo.failure("删除用户失败！", ex);
        }
    }
    @PostMapping("/update")//post请求要加@RequestBody进行接收
    public ResultVo update(@RequestBody Provider provider) {
        try {
            providerService.update(provider);
            return ResultVo.success("修改用户成功");
        } catch (Exception ex) {
            return ResultVo.failure("修改用户失败", ex);
        }

    }
    @GetMapping("/batchDel")
  /*  @RequiresPermissions("role:batchdel")*/
    public ResultVo batchDel( Integer [] ids, Provider provider, HttpSession session){
        try{
         List<Provider>  provider1= providerService.searchZeng(ids,provider,session);
            providerService.batchDel(ids);

            return  ResultVo.success("批量删除供应商成功!");
        }catch(Exception ex){
            return ResultVo.failure("批量删除失败!",ex);
        }
    }
}
